package com.zl.system.service.impl;

import com.zl.basic.service.impl.BaseServiceImpl;
import com.zl.system.domain.Menu;
import com.zl.system.mapper.MenuMapper;
import com.zl.system.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {


    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> menuTree() {
        List<Menu> all = menuMapper.loadAll();
        Map<Long,Menu> map  = new HashMap<>();
        for (Menu menu : all) {
            map.put(menu.getId(),menu);
        }

        List<Menu> menuTree = new ArrayList<>();
        for (Menu menu:all){
            if(menu.getParentId() == null){
                menuTree.add(menu);
            }else{
                Long parent_id = menu.getParentId();
                Menu parentMenu = map.get(parent_id);
                parentMenu.getChildren().add(menu);
            }
        }
        return menuTree;
    }
}
